package com.example.demo.controller;

import com.example.demo.common.AppResult;
import com.example.demo.common.SessionUtils;
import com.example.demo.exception.ApplicationException;
import com.example.demo.model.Task;
import com.example.demo.model.UserInfo;
import com.example.demo.service.TaskService;
import com.sun.istack.internal.NotNull;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-11-20
 * Time: 19:44
 */
@Api(tags = "Task 接口")
@RestController
@RequestMapping("/task")
public class TaskController {

    @Resource
    private TaskService taskService;

    /**
     * 添加任务
     */
    @ApiOperation("添加任务")
    @PostMapping("/add")
    public AppResult addTask(HttpServletRequest request,
                             @ApiParam("任务名") @RequestParam("name") @NotNull String name,
                             @ApiParam("截止时间") @RequestParam("date") @NotNull String date,
                             @ApiParam("地点") @RequestParam("place") @NotNull String place,
                             @ApiParam("备注") @RequestParam("remark") @NotNull String remark) {
        // 1.得到当前用户登录信息
        UserInfo user = SessionUtils.getUser(request);
        if (user == null) {
            return AppResult.failed("用户未登录!");
        }

        // 查询是否已存在
        Task taskByName = taskService.getTaskByName(name,user.getId());
        if (taskByName!=null) {
            return AppResult.failed("任务已存在!");
        }
        // 1.上面已经做了非空校验
        // 2.封装对象调用service
        Task task = new Task();
        task.setName(name);
        task.setDate(date);
        task.setPlace(place);
        task.setRemark(remark);
        task.setIsDone(0);
        task.setDeleteState(false);
        task.setUserId(user.getId());

        taskService.addTask(task);

        return AppResult.success();
    }

    /**
     * 查询所有未完成的任务
     */
    @ApiOperation("查询未完成的任务")
    @GetMapping("/gettodo")
    public AppResult<List<Task>> getAllTodoTask() {
        // 调用service
        List<Task> allTodoTask = taskService.getAllTodoTask();
        return AppResult.success(allTodoTask);
    }

    /**
     * 查询所有已完成的任务
     */
    @ApiOperation("查询已完成的任务")
    @GetMapping("/getdone")
    public AppResult<List<Task>> getAllDoneTask() {
        // 调用service
        List<Task> allDoneTask = taskService.getAllDoneTask();
        return AppResult.success(allDoneTask);
    }

    /**
     * 根据任务名更新任务
     */
    @ApiOperation("更新指定任务")
    @PostMapping("/update")
    public AppResult update(HttpServletRequest request,
                            @ApiParam("任务名") @RequestParam("name") @NotNull String name) {
        // 1.得到当前用户登录信息
        UserInfo user = SessionUtils.getUser(request);
        if (user == null) {
            return AppResult.failed("用户未登录!");
        }

        // 调用service
        taskService.updateTaskByNameP(name,user.getId());
        return AppResult.success();
    }

    /**
     * 删除指定任务，根据用户名
     */
    @ApiOperation("删除指定任务")
    @PostMapping("/delete")
    public AppResult deleteTask(HttpServletRequest request,
                                @ApiParam("任务名") @RequestParam("name") @NotNull String name) {
        // 1.得到当前用户登录信息
        UserInfo user = SessionUtils.getUser(request);
        if (user == null) {
            return AppResult.failed("用户未登录!");
        }

        // 调用service
        taskService.deleteTaskByName(name,user.getId());
        return AppResult.success();
    }

    /**
     * 获取所有任务
     */
    @ApiOperation("获取所有任务")
    @GetMapping("/getall")
    public AppResult<List<Task>> getAll(HttpServletRequest request) {
        // 1.得到当前用户登录信息
        UserInfo user = SessionUtils.getUser(request);
        if (user == null) {
            return AppResult.failed("用户未登录!");
        }

        // 调用service
        List<Task> all = taskService.getAll(user.getId());
        return AppResult.success(all);
    }

}
